//
//  EventCell.m
//  XPhoto
//
//  Created by baboy on 2019/8/8.
//  Copyright © 2019 baboy. All rights reserved.
//

#import "EventCell.h"
#import "Theme.h"
#import "UIButton+x.h"
#import "NSDate+x.h"
#import <AFNetworking/UIButton+AFNetworking.h>

#define SCHEME_NICKNAME  @"nickname://"
#define SCHEME_CONTENT @"content://"
#define VIEW_MARGIN 5
@implementation EventCell


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.contentTextView.textContainerInset = UIEdgeInsetsZero;
    self.contentTextView.linkTextAttributes = @{};
}
- (NSAttributedString *)contentAttributedString:(Event*)event{
    NSMutableAttributedString *nickNameAttrStr = [[NSMutableAttributedString alloc]
                                                initWithString:[NSString stringWithFormat:@"%@ ", event.operator.nickname]
                                                attributes:@{
                                                             NSFontAttributeName:THEME_TABLE_CELL_CONTENT_FONT,
                                                             NSForegroundColorAttributeName: [UIColor colorWithWhite:0.2 alpha:1]
                                                             }];
    [nickNameAttrStr addAttribute:NSLinkAttributeName value:SCHEME_NICKNAME range:NSMakeRange(0, nickNameAttrStr.length)];
    
    NSMutableAttributedString *contentAttrStr = [[NSMutableAttributedString alloc]
                                                 initWithString:event.content
                                                 attributes:@{
                                                              NSFontAttributeName: THEME_TABLE_CELL_CONTENT_FONT,
                                                              NSForegroundColorAttributeName: THEME_TABLE_CELL_CONTENT_COLOR
                                                              }];
    [contentAttrStr addAttribute:NSLinkAttributeName value:SCHEME_CONTENT range:NSMakeRange(0, contentAttrStr.length)];
    
    [nickNameAttrStr appendAttributedString:contentAttrStr];
    return nickNameAttrStr;
}
- (CGRect)contentBounds:(Event *)event{
    CGRect contentBounds = [[self contentAttributedString:event] boundingRectWithSize:CGSizeMake(self.bounds.size.width - 2*self.contentTextView.frame.origin.x, CGFLOAT_MAX)
                                                                                options:NSStringDrawingUsesLineFragmentOrigin
                                                                                context:nil];
    return contentBounds;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}
- (void)setEvent:(Event *)event{
    _event = event;
    self.contentTextView.attributedText = [self contentAttributedString: event];
    self.timeLabel.text = [event.createTime formatToHumanLang];
    [self.avatarButton setImageForState:UIControlStateNormal withURL:[NSURL URLWithString:event.operator.avatarThumb]];
}

#pragma UITextView delegate
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction{
    if( [[URL absoluteString] isEqualToString:SCHEME_NICKNAME] ){
        if (self.delegate && [self.delegate respondsToSelector:@selector(eventCellDidClickedNickName:)]) {
            [self.delegate eventCellDidClickedNickName:self];
            return NO;
        }
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(eventCellDidClickedContent:)]) {
        [self.delegate eventCellDidClickedContent:self];
        return NO;
    }
    DLOG(@"%@,%@",[URL scheme], NSStringFromRange(textView.selectedRange));
    return NO;
}

- (IBAction)clickAvatarButton:(id)sender{
    if (self.delegate && [self.delegate respondsToSelector:@selector(eventCellDidClickedAvatar:)]) {
        [self.delegate eventCellDidClickedAvatar:self];
    }
}
- (CGFloat)heightForEvent:(Event*)event width:(CGFloat)width{
    //from photoScrollViewbottom
    CGFloat h = self.contentTextView.frame.origin.y;
    h += [self contentBounds:event].size.height+4;
    h += self.timeLabel.bounds.size.height;
    h += VIEW_MARGIN;
    return h;
}
@end
